package net.lcheng.utils;

import com.alibaba.fastjson.JSON;
import net.lcheng.model.OOrder;
import net.lcheng.vo.OrderModel;
import net.lcheng.vo.OrderProductsEntity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by LV on 2016/7/5.
 */
public class Oorder2OrderModel {
    private static Oorder2OrderModel ourInstance = new Oorder2OrderModel();

    private Oorder2OrderModel() {}

    private List<OrderModel> getOrderModelList(List<OOrder> list){
        if (list!=null&&list.size()>0) {
            List<OrderModel> orderModelList = new ArrayList<>();
//
            for (int i = 0; i < list.size(); i++) {
                OrderModel orderModel = new OrderModel();
                orderModel.setoOrder(list.get(i));
                //将json串类型的orderItems转换成OrderProductsEntity对象
                List<OrderProductsEntity> entities = JSON.parseArray(list.get(i).getOrderProducts(), OrderProductsEntity.class);
                int productNum=0;
                for (OrderProductsEntity entity:entities){
                    productNum+=entity.getQuantity();
                }
                orderModel.setOrderProductNum(productNum);
                orderModel.setOrderProductsEntityList(entities);
                orderModelList.add(orderModel);
            }
            return orderModelList;
        }
        return null;
    }

    private OrderModel getOrderModel(OOrder oOrder){
        if (oOrder!=null) {
            OrderModel orderModel = new OrderModel();
            orderModel.setoOrder(oOrder);
            //将json串类型的orderItems转换成OrderProductsEntity对象
            List<OrderProductsEntity> entities = JSON.parseArray(oOrder.getOrderProducts(), OrderProductsEntity.class);
            int productNum=0;
            for (OrderProductsEntity entity:entities){
                productNum+=entity.getQuantity();
            }
            orderModel.setOrderProductNum(productNum);
            orderModel.setOrderProductsEntityList(entities);

            return orderModel;
        }
        return null;
    }

    //将List<OOrder>转换成List<OrderModel>
    public static List<OrderModel> getOorder2OrderModel(List<OOrder> list){
        return ourInstance.getOrderModelList(list);
    }

    //将OOrder转换成OrderModel
    public static OrderModel getOorder2OrderModel(OOrder oOrder){return ourInstance.getOrderModel(oOrder);}
}
